package com.augurit.common.system.convert;

import java.util.ArrayList;
import java.util.List;

import org.springside.modules.web.struts2.Struts2Utils;

import com.augurit.common.base.extmodel.BasicCombo;
import com.augurit.common.base.extmodel.ExtStoreMenu;
import com.augurit.common.base.extmodel.base.BaseTreeNode;
import com.augurit.common.base.extmodel.base.ExtElement;
import com.augurit.common.base.extmodel.base.LinkElement;
import com.augurit.common.base.extmodel.base.ITreeNode;
import com.augurit.common.base.extmodel.base.LinkTreeNode;
import com.augurit.common.base.extmodel.inner.MenuCheckTreeNode;
import com.augurit.common.base.extmodel.inner.MenuLinkTreeNode;
import com.augurit.common.constant.OrgConstant;
import com.augurit.common.entity.AcMenu;
import com.augurit.common.entity.AcUserShortcut;
import com.augurit.common.system.util.OrgUtils;
import com.augurit.common.system.util.TreeUtils;
import com.augurit.common.system.web.org.form.AcMenuForm;
import com.augurit.common.system.web.org.form.AcUserShortcutForm;

public class AcMenuConverter {

	/**
	 * 将AcMenu对象列表转换为指定对象类型对象列表
	 * @param nodeType 要转换的对象类型
	 * @param list AcMenu对象列表
	 * @return
	 */
	public static List convertByNodeType(String nodeType, List<AcMenu> list){
		return convertByNodeType(nodeType, list, false);
	}
	
	public static List convertByNodeType(String nodeType, List<AcMenu> list, int depth){
		return convertByNodeType(nodeType, list, depth > 1 ? true : false);
	}
	
	/**
	 * 将AcMenu对象列表转换为指定对象类型对象列表
	 * @param nodeType 要转换的对象类型
	 * @param list AcMenu对象列表
	 * @return
	 */
	public static List convertByNodeType(String nodeType, List<AcMenu> list, boolean construtTree){
		List result = null;
		
		if(nodeType != null && nodeType.trim().length() > 0){

			if(nodeType.equals(ExtStoreMenu.class.getSimpleName()))
				result = convertToExtStoreMenu(list);
			
			else if(nodeType.equals(MenuLinkTreeNode.class.getSimpleName()))
				result = convertToMenuLinkTreeNode(list);
			
			else if(nodeType.equals(MenuCheckTreeNode.class.getSimpleName()))
				result = convertToMenuCheckTreeNode(list);
			
			else if(nodeType.equals(LinkTreeNode.class.getSimpleName()))
				result = convertToLinkTreeNode(list);
			
			else if(nodeType.equals(BaseTreeNode.class.getSimpleName()))
				result = convertToBaseTreeNode(list);

			else if(nodeType.equals(AcMenuForm.class.getSimpleName()))
				result = convertToFormTree(list, construtTree);
			
			else if(nodeType.equals(ExtElement.class.getSimpleName()))
				result = convertToExtElement(list);
		}
		
		return result;
	}
	
	/**
	 * 转换为以树结构组织的AcMenuForm对象列表
	 * @param list
	 * @param depth
	 * @return
	 */
	public static List<AcMenuForm> convertToFormTree(List<AcMenu> list, boolean construtTree){
		//转换为form对象列表
		List<AcMenuForm> tempList = convertToForm(list);
		
		if(!construtTree)
			return tempList;
		else
			return TreeUtils.constructMenuTree(tempList);
	}
	
	/**
	 * 将AcMenu对象列表转换为AcMenuForm对象列表
	 * @param list
	 * @return
	 */
	public static List<AcMenuForm> convertToForm(List<AcMenu> list){
		List<AcMenuForm> result = null;
		
		if(list != null && list.size() > 0){
			result = new ArrayList();
			
			for(AcMenu entity : list){
				result.add(convertToForm(entity));
			}
		}
		
		return result;
	}
	
	/**
	 * 将AcMenu对象转换为AcMenuForm对象
	 * @param entity AcMenu对象
	 * @return
	 */
	public static AcMenuForm convertToForm(AcMenu entity){
		return convertToForm(entity, true);
	}
	
	/**
	 * 将AcMenu对象转换为AcMenuForm对象
	 * @param entity AcMenu对象
	 * @return
	 */
	public static AcMenuForm convertToForm(AcMenu entity, boolean needConvert){
		if(entity != null){
			AcMenuForm form = new AcMenuForm();
			form.setMenuId(entity.getMenuId());
			form.setMenuCode(entity.getMenuCode());
			form.setMenuName(entity.getMenuName());
			form.setParentMenuId(entity.getParentMenuId());
			form.setMenuLevel(entity.getMenuLevel());
			form.setMenuSeq(entity.getMenuSeq());
			form.setAppId(entity.getAppId());
			form.setFuncId(entity.getFuncId());
			form.setIsLeaf(entity.getIsLeaf());
			form.setSubCount(entity.getSubCount());
			form.setIsActive(entity.getIsActive());
			form.setSortNo(entity.getSortNo());
			form.setOpenMode(entity.getOpenMode());
			form.setMenuDesc(entity.getMenuDesc());
			form.setRemark(entity.getRemark());
			
			form.setMenuUrl(OrgUtils.getMenuUrl(entity.getMenuUrl(), needConvert));
			form.setSmallImgPath(OrgUtils.getImgPath(entity.getSmallImgPath(), needConvert));
			form.setMiddleImgPath(OrgUtils.getImgPath(entity.getMiddleImgPath(), needConvert));
			form.setBigImgPath(OrgUtils.getImgPath(entity.getBigImgPath(), needConvert));
			
			return form;
		}else
			return null;
	}
	
	/**
	 * 将AcMenu对象转换为AcMenuForm对象
	 * @param entity AcMenu对象
	 * @return
	 */
	public static void convertToEntity(AcMenuForm form, AcMenu entity){
		if(form != null && entity != null){
			
			if(form.getMenuId() != null)
				entity.setMenuId(form.getMenuId());
			
			if(form.getMenuCode() != null && form.getMenuCode().trim().length() > 0)
				entity.setMenuCode(form.getMenuCode());
			
			if(form.getMenuName() != null && form.getMenuName().trim().length() > 0)
				entity.setMenuName(form.getMenuName());
			
			if(form.getParentMenuId() != null)
				entity.setParentMenuId(form.getParentMenuId());
			
			if(form.getMenuLevel() != null)
				entity.setMenuLevel(form.getMenuLevel());
			
			if(form.getMenuSeq() != null && form.getMenuSeq().trim().length() > 0)
				entity.setMenuSeq(form.getMenuSeq());
			
			if(form.getMenuUrl() != null && form.getMenuUrl().trim().length() > 0)
				entity.setMenuUrl(form.getMenuUrl());
			
			if(form.getAppId() != null)
				entity.setAppId(form.getAppId());
			
			if(form.getFuncId() != null)
				entity.setFuncId(form.getFuncId());
			
			if(form.getIsLeaf() != null && form.getIsLeaf().trim().length() > 0)
				entity.setIsLeaf(form.getIsLeaf());
			else
				entity.setIsLeaf(OrgConstant.ONE);
			
			if(form.getSubCount() != null)
				entity.setSubCount(form.getSubCount());
			else
				entity.setSubCount(0);
			
			if(form.getIsActive() != null && form.getIsActive().trim().length() > 0)
				entity.setIsActive(form.getIsActive());
			else
				entity.setIsActive(OrgConstant.ONE);
			
			if(form.getSortNo() != null)
				entity.setSortNo(form.getSortNo());
			
			if(form.getOpenMode() != null && form.getOpenMode().trim().length() > 0)
				entity.setOpenMode(form.getOpenMode());
			
			if(form.getSmallImgPath() != null && form.getSmallImgPath().trim().length() > 0)
				entity.setSmallImgPath(form.getSmallImgPath());
			
			if(form.getMiddleImgPath() != null && form.getMiddleImgPath().trim().length() > 0)
				entity.setMiddleImgPath(form.getMiddleImgPath());
			
			if(form.getBigImgPath() != null && form.getBigImgPath().trim().length() > 0)
				entity.setBigImgPath(form.getBigImgPath());
			
			if(form.getMenuDesc() != null && form.getMenuDesc().trim().length() > 0)
				entity.setMenuDesc(form.getMenuDesc());
			
			if(form.getRemark() != null && form.getRemark().trim().length() > 0)
				entity.setRemark(form.getRemark());
		}
	}
	
	/**
	 * 将AcMenu对象列表转换为ExtTreeNode对象列表
	 * @param list AcMenu对象列表
	 * @return
	 */
	public static List<MenuCheckTreeNode> convertToMenuCheckTreeNode(List<AcMenu> list){
		List<MenuCheckTreeNode> result = null;
		
		if(list != null && list.size() > 0){
			result = new ArrayList();
			
			for(AcMenu entity : list){
				MenuCheckTreeNode node = new MenuCheckTreeNode();
				convertToMenuCheckTreeNode(entity, node);
				result.add(node);
			}
		}
		
		return result;
	}
	
	/**
	 * 将AcMenu对象列表转换为ExtStoreMenuNode对象列表
	 * @param list AcMenu对象列表
	 * @return
	 */
	public static List<ExtStoreMenu> convertToExtStoreMenu(List<AcMenu> list){
		List<ExtStoreMenu> result = null;
		
		if(list != null && list.size() > 0){
			result = new ArrayList();
			
			for(AcMenu entity : list){
				ExtStoreMenu node = new ExtStoreMenu();
				convertToExtStoreMenu(entity, node);
				result.add(node);
			}
		}
		
		return result;
	}
	
	/**
	 * 将AcMenu对象的属性值转换到ExtStoreMenuNode对象中
	 * @param entity AcMenu对象
	 * @param node ExtStoreMenuNode对象
	 */
	public static void convertToExtStoreMenu(AcMenu entity, ExtStoreMenu node){
		if(entity != null && node != null){
			convertToExtElement(entity, node);
			
			boolean isLeaf = entity.getIsLeaf() != null && entity.getIsLeaf().equals(OrgConstant.ONE) ? true : false;
			if(isLeaf){
				node.setCls(ITreeNode.DEFAULT_LEAF_TREE_NODE_CLS);
				node.setLeaf(true);
				node.setUrl(OrgUtils.getMenuUrl(entity.getMenuUrl(), true));
				node.setHandler("onLinkClick");
			}else{
				node.setCls(ITreeNode.DEFAULT_NOT_LEAF_TREE_NODE_CLS);
				node.setLeaf(false);
				node.setMenu("new Ext.ux.menu.StoreMenu({url:'index!getChildren.action', baseParams:{id: '" + entity.getMenuId() + "'}})");
			}
			node.setMenuLevel(entity.getMenuLevel());
			node.setTooltip(entity.getMenuDesc());
		}
	}
	


	/**
	 * 将AcMenu对象的属性值转换到ExtBaseTreeNode对象中
	 * @param entity AcMenu对象
	 * @param node ExtBaseTreeNode对象
	 */
	public static void convertToBaseTreeNode(AcMenu entity, BaseTreeNode node){
		if(entity != null && node != null){
			convertToExtElement(entity, node);
			
			boolean isLeaf = entity.getIsLeaf() != null && entity.getIsLeaf().equals(OrgConstant.ONE) ? true : false;
			if(isLeaf){
				node.setCls(ITreeNode.DEFAULT_LEAF_TREE_NODE_CLS);
				node.setLeaf(true);
			}else{
				node.setCls(ITreeNode.DEFAULT_NOT_LEAF_TREE_NODE_CLS);
				node.setLeaf(false);
			}
			
			if(entity.getMenuDesc() != null && entity.getMenuDesc().trim().length() > 0)
				node.setQtip(entity.getMenuDesc());
		}
	}
	
	/**
	 * 将AcMenu对象的属性值转换到CheckTreeNode对象中
	 * @param entity
	 */
	public static void convertToMenuCheckTreeNode(AcMenu entity, MenuCheckTreeNode node){
		if(entity != null && node != null){
			convertToBaseTreeNode(entity, node);
			
			node.setChecked(false);
			if(entity.getFuncId() != null)
				node.setAssociateFunc(true);
			else
				node.setAssociateFunc(false);
		}
	}
	
	/**
	 * 将AcMenu对象列表转换为ExtBasicTreeNode对象列表
	 * @param list AcMenu对象列表
	 * @return
	 */
	public static List<BaseTreeNode> convertToBaseTreeNode(List<AcMenu> list){
		List<BaseTreeNode> result = null;
		
		if(list != null && list.size() > 0){
			result = new ArrayList();
			
			for(AcMenu entity : list){
				BaseTreeNode node = new BaseTreeNode();
				convertToBaseTreeNode(entity, node);
				result.add(node);
			}
		}
		
		return result;
	}

	public static List<MenuLinkTreeNode> convertToMenuLinkTreeNode(List<AcMenu> list){
		List<MenuLinkTreeNode> result = null;
		
		if(list != null && list.size() > 0){
			result = new ArrayList();
			
			for(AcMenu entity : list){
				MenuLinkTreeNode node = new MenuLinkTreeNode();
				convertToMenuLinkTreeNode(entity, node);
				result.add(node);
			}
		}
		
		return result;
	}
	
	public static void convertToMenuLinkTreeNode(AcMenu entity, MenuLinkTreeNode node){
		if(entity != null && node != null){
			convertToLinkTreeNode(entity, node);

			node.setLevel(entity.getMenuLevel());
			node.setParentId(entity.getParentMenuId() != null ? entity.getParentMenuId().toString() : null);
		}
	}
	
	public static List<LinkTreeNode> convertToLinkTreeNode(List<AcMenu> list){
		List<LinkTreeNode> result = null;
		
		if(list != null && list.size() > 0){
			result = new ArrayList();
			
			for(AcMenu entity : list){
				LinkTreeNode node = new LinkTreeNode();
				convertToLinkTreeNode(entity, node);
				result.add(node);
			}
		}
		
		return result;
	}
	
	public static void convertToLinkTreeNode(AcMenu entity, LinkTreeNode node){
		if(entity != null && node != null){
			convertToBaseTreeNode(entity, node);
			
			node.setUrl(OrgUtils.getMenuUrl(entity.getMenuUrl(), true));
		}
	}
	
	/**
	 * 将AcMenu对象列表转换为ExtCommonNode对象列表
	 * @param list AcMenu对象列表
	 * @return
	 */
	public static List<LinkElement> convertToExtLinkElement(List<AcMenu> list){
		List<LinkElement> result = null;
		
		if(list != null && list.size() > 0){
			result = new ArrayList();
			
			for(AcMenu entity : list){
				LinkElement node = new LinkElement();
				convertToExtLinkElement(entity, node);
				result.add(node);
			}
		}
		
		return result;
	}

	/**
	 * 将AcMenu对象的属性值转换到ExtCommonNode对象中
	 * @param entity AcMenu对象
	 * @param node ExtCommonNode对象
	 */
	public static void convertToExtLinkElement(AcMenu entity, LinkElement node){
		if(entity != null && node != null){
			convertToExtElement(entity, node);
			
			node.setUrl(OrgUtils.getMenuUrl(entity.getMenuUrl(), true));
		}
	}

	public static List<ExtElement> convertToExtElement(List<AcMenu> list){
		List<ExtElement> result = null;
		
		if(list != null && list.size() > 0){
			result = new ArrayList();
			
			for(AcMenu entity : list){
				ExtElement node = new ExtElement();
				convertToExtElement(entity, node);
				result.add(node);
			}
		}
		
		return result;
	}
	
	public static void convertToExtElement(AcMenu entity, ExtElement node){
		if(entity != null && node != null){
			node.setId(entity.getMenuId().toString());
//			node.setMenuCode(entity.getMenuCode());
			node.setText(entity.getMenuName());
			node.setIcon(Struts2Utils.getRequest().getContextPath() + entity.getSmallImgPath());
		}
	}
	
	public static List<BasicCombo> convertToBasicCombo(List<AcMenuForm> list, boolean allowBlank){
		List<BasicCombo> result = new ArrayList();
		
		if(allowBlank)
			result.add(BasicCombo.getBlankItem());
		
		if(list != null && list.size() > 0){
			for(AcMenuForm form : list){
				BasicCombo combo = new BasicCombo();
				combo.setLabel(form.getMenuName());
				combo.setValue(form.getMenuId().toString());
				result.add(combo);
			}
		}
		
		return result;
		
	}
	
	public static AcUserShortcutForm convertToForm(AcUserShortcut entity){
		if(entity != null){
			AcUserShortcutForm form = new AcUserShortcutForm();
			form.setUsId(entity.getUsId());
			form.setUserId(entity.getUserId());
			form.setMenuId(entity.getMenuId());
			form.setSortNo(entity.getSortNo());
			return form;
		}else
			return null;
	}
	
	public static void convertToEntity(AcUserShortcutForm form, AcUserShortcut entity){
		if(form != null && entity != null){

			if(form.getUsId() != null)
				entity.setUsId(form.getUsId());
			
			if(form.getUserId() != null)
				entity.setUserId(form.getUserId());
			
			if(form.getMenuId() != null)
				entity.setMenuId(form.getMenuId());
			
			if(form.getSortNo() != null)
				entity.setSortNo(form.getSortNo());
		}
	}
}
